package com.gui.listeners;

import com.util.ResourceLoader;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

/**
 * Listener class for the CustomTableModel binding categoriesTable JTable
 * component.
 * @author sithum
 */
public class CategoriesTableModelListener implements TableModelListener{

    JTextField categoryName;
    JTable table;
    int reflectColumn;

    public CategoriesTableModelListener(JTextField categoryName, JTable table) {
        this.categoryName = categoryName;
        this.table = table;
        this.reflectColumn = Integer.parseInt(ResourceLoader.getInstance().getProperty("category.edit.reflect"));
        table.getModel().addTableModelListener(this);
    }
    
    public void tableChanged(TableModelEvent e) {
        int selectedRow = e.getFirstRow();
        int selectedColumn = e.getColumn();
        Object changedValue = table.getValueAt(selectedRow, selectedColumn);
        if (changedValue.toString().equalsIgnoreCase("true")) {
            System.out.println(table.getValueAt(selectedRow, reflectColumn).toString());
            categoryName.setText(table.getValueAt(selectedRow, reflectColumn).toString());
        } else if (changedValue.toString().equalsIgnoreCase("false")) {
            categoryName.setText("");
        }
    }

}
